package stark.app.function.print;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.blankj.utilcode.util.ImageUtils;
import com.hjq.bar.OnTitleBarListener;
import com.stark.camera.kit.id.IdCameraCallback;
import com.stark.camera.kit.id.IdCameraFactory;
import com.stark.print.lib.Printer;

import java.util.List;

import stark.app.R;
import stark.app.databinding.ActivityPrintIdBinding;
import stark.common.basic.base.BaseNoModelActivity;
import stark.common.basic.utils.IntentUtil;

/**
 * Author：daizhongbin on 2022/8/5
 * Email：1027154338@qq.com
 * Desc:
 */
public class PrintIdActivity extends BaseNoModelActivity<ActivityPrintIdBinding> {

    public static final int TYPE_ID_CARD = 1;   //身份证
    public static final int TYPE_ID_YYZZ = 2;   //营业执照
    public static final int TYPE_ID_DRIVE = 3;  //驾驶证
    public static final int TYPE_ID_BANK = 4;   //银行卡

    private static List<Bitmap> sBitmaps;
    private static int sType;

    public static void start(Context context, List<Bitmap> bitmaps, int type) {
        Intent intent = IntentUtil.getIntent(context, PrintIdActivity.class);
        sBitmaps = bitmaps;
        sType = type;
        context.startActivity(intent);
    }

    @Override
    protected int onCreate() {
        return R.layout.activity_print_id;
    }

    @Override
    protected void initView() {
        //titlebar
        mDataBinding.titleBar.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View view) {
                onBackPressed();
            }

            @Override
            public void onTitleClick(View view) {
            }

            @Override
            public void onRightClick(View view) {
                Bitmap bitmap = ImageUtils.view2Bitmap(mDataBinding.flContent);
                Printer.printBitmap(PrintIdActivity.this, "自己命名", bitmap);
            }
        });

        int contentLayoutId = R.layout.layout_id_idcard;
        mDataBinding.titleBar.setTitle("身份证");
        if (sType == TYPE_ID_YYZZ) {
            contentLayoutId = R.layout.layout_id_yyzz;
            mDataBinding.titleBar.setTitle("营业执照");
        } else if (sType == TYPE_ID_DRIVE) {
            contentLayoutId = R.layout.layout_id_drive;
            mDataBinding.titleBar.setTitle("驾驶证");
        } else if (sType == TYPE_ID_BANK) {
            contentLayoutId = R.layout.layout_id_drive;
            mDataBinding.titleBar.setTitle("银行卡");
        }
        View viewContent = LayoutInflater.from(this).inflate(contentLayoutId, mDataBinding.flContent);
        initViewContent(viewContent);
    }

    @Override
    protected void initData() {

    }

    private void initViewContent(View parent) {
        if (sBitmaps == null || sBitmaps.size() == 0) return;

        ImageView ivFront = parent.findViewById(R.id.ivFront);
        ImageView ivBackground = parent.findViewById(R.id.ivBackground);
        ImageView ivId = parent.findViewById(R.id.ivId);

        if (ivFront != null) {
            ivFront.setOnClickListener(this);
            ivFront.setImageBitmap(sBitmaps.get(0));
        }
        if (ivBackground != null) {
            ivBackground.setOnClickListener(this);
            if (sBitmaps.size() >= 2) {
                ivBackground.setImageBitmap(sBitmaps.get(1));
            }
        }
        if (ivId != null) {
            ivId.setOnClickListener(this);
            ivId.setImageBitmap(sBitmaps.get(0));
        }
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivFront) {
            clickFront();
        } else if (id == R.id.ivBackground) {
            clickBackground();
        } else if (id == R.id.ivId) {
            clickIvId();
        }
    }

    private void clickFront() {
        IdCameraCallback callback = new IdCameraCallback() {
            @Override
            public void onResult(List<Bitmap> bitmaps) {
                if (bitmaps == null || bitmaps.size() == 0) {
                    return;
                }
                ImageView ivFront = findViewById(R.id.ivFront);
                ivFront.setImageBitmap(bitmaps.get(0));
            }
        };
        if (sType == TYPE_ID_CARD) {
            IdCameraFactory.takeIdCardP(this, callback);
        } else if (sType == TYPE_ID_DRIVE) {
            IdCameraFactory.takeIdDriveOne(this, callback);
        } else if (sType == TYPE_ID_BANK) {
            IdCameraFactory.takeIdBankOne(this, callback);
        }
    }

    private void clickBackground() {
        IdCameraCallback callback = new IdCameraCallback() {
            @Override
            public void onResult(List<Bitmap> bitmaps) {
                if (bitmaps == null || bitmaps.size() == 0) {
                    return;
                }
                ImageView ivBackground = findViewById(R.id.ivBackground);
                ivBackground.setImageBitmap(bitmaps.get(0));
            }
        };
        if (sType == TYPE_ID_CARD) {
            IdCameraFactory.takeIdCardN(this, callback);
        } else if (sType == TYPE_ID_DRIVE) {
            IdCameraFactory.takeIdDriveOne(this, callback);
        } else if (sType == TYPE_ID_BANK) {
            IdCameraFactory.takeIdBankOne(this, callback);
        }
    }

    private void clickIvId() {
        IdCameraFactory.takeIdYyzz(this, new IdCameraCallback() {
            @Override
            public void onResult(List<Bitmap> bitmaps) {
                if (bitmaps == null || bitmaps.size() == 0) {
                    return;
                }
                ImageView ivId = findViewById(R.id.ivId);
                ivId.setImageBitmap(bitmaps.get(0));
            }
        });
    }
}
